package com.yyhu.auto;

import com.google.crypto.tink.subtle.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

public class RsaHelper {
    String RSA = "RSA"; // 非对称加密密钥算法

    String publicKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDJmt0IHCkUzzAF" +
            "iUx7nfwG9ShjpGNVqOiipr78MJxQIIaSqd2S81fkJeY3eqS9CAt1P0UYEUnNFUKx" +
            "R+NdjU5VaZqk7z1Yv4NUQa1oGgWJDity3hHr/L8tRe9/DoPbhHY4RDU2WBkBIA5r" +
            "uG4CnTdBlK9wHpinSYs0KCwHVNieWD0BnBOGD2Ph28JFUfAlEj7/D/s0C8TlTSx8" +
            "pBKnUTUaZqotPNaKUczop7iocL5wYtR/2j20IOMllxO7PP4fSMMKgtVZhE3iAnM1" +
            "Jq3tgi/JXJQF47NWagK5PiG+HnymJ6G7nnU1gRLb2z5wUITtT7qBXW/L/dww/6Et" +
            "ECg8ieufAgMBAAECggEBAJ8lsCyRxmBV+iu0qfq/yU9Ywox+HahmUVbs2MFOQmcC" +
            "dhlbHL2/eXdx752GJHvQ2PKygoGwTBkoGmDGRhb40Iied+SRs1TWwf6GZjRSrmH2" +
            "HJht86nd+LDtj1U2iSKGG6jpNzRyMTLxMlEY40cPAqqzjBavIISWitwehjs3YFUG" +
            "HH3gfp4VzZDd7uwXAEZx1eu8vptSTUIGbnaqZrKzu6xm1vmcATT3qwmnXAJdOtXN" +
            "hOCxNTS6yU4s8fb4phexSpgfW0jpGmSrDVlZzNxFA2qo2gpPaGxy19n3cvopwTu4" +
            "MaUQrbLaUrLxYTrUgsU/blPCOOH8k/1lOH+FocIBwHkCgYEA+5qWCAC0IJD7PH4S" +
            "xh/xbQTHP29TQe9q+HGDSZ+3IIWu/Q2bdxqjDE6e4yh5c+fxva/brrWMzZutELig" +
            "gpRURayeK1iDQkEHa6F4WlOGmf2+HyVBZ6yezg3miKpQkXrGZi4Q/ATgkn4rYAes" +
            "iALn1ASl5VvfNeDE/ygpYeCd44UCgYEAzSCiWq5NMlmQU5zFXXII36WgGpKOLlVI" +
            "htKwQRq/6ILArOOGBkSkmnaukuWukzGNixsyuBNToeb4cfsmsW2TOsVErcNn4mLM" +
            "UU817vuHc0IGfvTTY48s8qo6Mgp7rQ+h8xa+E6Nv6pWw/wMT/+A8ck9oXCVIzeZe" +
            "f6eOYxQKYdMCgYBPa3F2Xzp8mSck5iro7UZZAkweovZl9rwOZk5p3AeDNY+SA0yS" +
            "oQ7coKiYzbrgslNla/BtSpOCh2Ruxcisz0N2ptFan1r3e+1qtRJ3eKwhSa5MyIbH" +
            "wf5yzoJ5m0FJWrbrpBjqZGO6+iaY7nMwj7I8A6eWKZTgve2RGU1Ovwc3vQKBgHIX" +
            "yLXvZPiwYFphRFWpa84ZbWfaREL5JJH49hgALOXCdDC1jIJIA9X2VTtXjTZJxZSt" +
            "2BQSi93tfhrHusnNTKrPtld/7HQ6x7IiALmgJcKx4rMSsdEafcGrdRsIQaoMm+N9" +
            "rtN1gHZWNFBjAei8bGKYUE+QXWJI1B7kr4nkR7IJAoGBAJEqCSabJ1ftEobXzS29" +
            "4qq8RxK3au8JAJQ2CHkGzreOCATHtuEbYpoXhQ9nyd+2W0XkGvZEUOTh9JSXXZs9" +
            "Yk+AB2LLmp5VndAJlWdiGiww/fjXmmvi+/LmILU+gFifxlRWZXTUhwL1Soa02My5" +
            "xzIP34sv6iFQDfTGXu2u/zlz";


    PrivateKey getPrivateKey() {
        byte[] decoded = Base64.decode(publicKey.getBytes(), Base64.DEFAULT);
        KeySpec keySpec = new PKCS8EncodedKeySpec(decoded);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(RSA);
            return keyFactory.generatePrivate(keySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * ./gradlew AutoBuildApk -PbuildParam=sunmi#hideIcon#Release -PtmpAccount
     * =yangyang.hu  -PtmpPassword=wnYs9ObD+AAORl3feNsrZNmJfi79TY8XzW3Cpgql8boeOxsp6FYnNSR+z/7+XrfK1Wx3zARpSiqboh9bM9ka6gNNu+OpXR6Jkmwwv8CP5VS0MTeUimpoz7Oetsly2+/Cqb/ymKWzCBqYUK0yH6c6ubjd+1+8jazyEAR25jGp7YHwZNtfqdH0xyMNjMiCFjTRmIT/lTRJWFukroDX9n9yBVRtmpy5WA3rwMp9BzavzbizyE74L8kJdyAQcMiTljG2eTStYSAlGpjTUdguX8MpFcv0AfJhHAvFZYLXWaxFZASmELs207R6QJP7UiasR9gxQm6sjUiI4m91D0Oj6EMjIw==
     * @param encryptedData
     * @return
     */
    // RSA解密
    String rsaDecrypt(String encryptedData) {
        PrivateKey privateKey = getPrivateKey();
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decoded = Base64.decode(encryptedData.getBytes(), Base64.DEFAULT);
            byte[] decryptedBytes = cipher.doFinal(decoded);
            return new String(decryptedBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
